import type { VbenFormSchema } from '#/adapter/form';
import type { VxeTableGridOptions } from '#/adapter/vxe-table';

import { CommonStatusEnum, DICT_TYPE } from '@vben/constants';
import { getDictOptions } from '@vben/hooks';

import { $t } from '#/locales';
import { z } from '#/adapter/form';

/** 新增/修改的表单 */
export function useFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'id',
      component: 'Input',
      dependencies: {
        triggerFields: [''],
        show: () => false,
      },
    },
    {
      fieldName: 'clientId',
      label: $t('views.system.oauth2.client.clientId'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.clientIdPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'secret',
      label: $t('views.system.oauth2.client.secret'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.secretPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'name',
      label: $t('views.system.oauth2.client.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.namePlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'logo',
      label: $t('views.system.oauth2.client.logo'),
      component: 'ImageUpload',
      rules: 'required',
    },
    {
      fieldName: 'description',
      label: $t('views.system.oauth2.client.description'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.descriptionPlaceholder'),
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'RadioGroup',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        buttonStyle: 'solid',
        optionType: 'button',
      },
      rules: z.number().default(CommonStatusEnum.ENABLE),
    },
    {
      fieldName: 'accessTokenValiditySeconds',
      label: $t('views.system.oauth2.client.accessTokenValiditySeconds'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.accessTokenValiditySecondsPlaceholder'),
        min: 0,
      },
      rules: 'required',
    },
    {
      fieldName: 'refreshTokenValiditySeconds',
      label: $t('views.system.oauth2.client.refreshTokenValiditySeconds'),
      component: 'InputNumber',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.refreshTokenValiditySecondsPlaceholder'),
        min: 0,
      },
      rules: 'required',
    },
    {
      fieldName: 'authorizedGrantTypes',
      label: $t('views.system.oauth2.client.authorizedGrantTypes'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.SYSTEM_OAUTH2_GRANT_TYPE),
        mode: 'multiple',
        placeholder: $t('views.system.oauth2.client.authorizedGrantTypesPlaceholder'),
      },
      rules: 'required',
    },
    {
      fieldName: 'scopes',
      label: $t('views.system.oauth2.client.scopes'),
      component: 'Select',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.scopesPlaceholder'),
        mode: 'tags',
        allowClear: true,
      },
    },
    {
      fieldName: 'autoApproveScopes',
      label: $t('views.system.oauth2.client.autoApproveScopes'),
      component: 'Select',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.autoApproveScopesPlaceholder'),
        mode: 'multiple',
      },
      dependencies: {
        triggerFields: ['scopes'],
        componentProps: (values) => ({
          options: values.scopes
            ? values.scopes.map((scope: string) => ({
                label: scope,
                value: scope,
              }))
            : [],
        }),
      },
    },
    {
      fieldName: 'redirectUris',
      label: $t('views.system.oauth2.client.redirectUris'),
      component: 'Select',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.redirectUrisPlaceholder'),
        mode: 'tags',
      },
      rules: 'required',
    },
    {
      fieldName: 'authorities',
      label: $t('views.system.oauth2.client.authorities'),
      component: 'Select',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.authoritiesPlaceholder'),
        mode: 'tags',
      },
    },
    {
      fieldName: 'resourceIds',
      label: $t('views.system.oauth2.client.resourceIds'),
      component: 'Select',
      componentProps: {
        mode: 'tags',
        placeholder: $t('views.system.oauth2.client.resourceIdsPlaceholder'),
      },
    },
    {
      fieldName: 'additionalInformation',
      label: $t('views.system.oauth2.client.additionalInformation'),
      component: 'Textarea',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.additionalInformationPlaceholder'),
      },
    },
  ];
}

/** 列表的搜索表单 */
export function useGridFormSchema(): VbenFormSchema[] {
  return [
    {
      fieldName: 'name',
      label: $t('views.system.oauth2.client.name'),
      component: 'Input',
      componentProps: {
        placeholder: $t('views.system.oauth2.client.namePlaceholder')
        allowClear: true,
      },
    },
    {
      fieldName: 'status',
      label: $t('common.status'),
      component: 'Select',
      componentProps: {
        options: getDictOptions(DICT_TYPE.COMMON_STATUS, 'number'),
        allowClear: true,
        placeholder: $t('common.statusPlaceholder'),
      },
    },
  ];
}

/** 列表的字段 */
export function useGridColumns(): VxeTableGridOptions['columns'] {
  return [
    { type: 'checkbox', width: 40 },
    {
      field: 'clientId',
      title: $t('views.system.oauth2.client.clientId'),
      minWidth: 120,
    },
    {
      field: 'secret',
      title: $t('views.system.oauth2.client.secret'),
      minWidth: 120,
    },
    {
      field: 'name',
      title: $t('views.system.oauth2.client.name'),
      minWidth: 120,
    },
    {
      field: 'logo',
      title: $t('views.system.oauth2.client.logo'),
      minWidth: 100,
      cellRender: {
        name: 'CellImage',
      },
    },
    {
      field: 'status',
      title: $t('common.status'),
      minWidth: 80,
      cellRender: {
        name: 'CellDict',
        props: { type: DICT_TYPE.COMMON_STATUS },
      },
    },
    {
      field: 'accessTokenValiditySeconds',
      title: $t('views.system.oauth2.client.accessTokenValiditySeconds'),
      minWidth: 150,
      formatter: ({ cellValue }) => `${cellValue} 秒`,
    },
    {
      field: 'refreshTokenValiditySeconds',
      title: $t('views.system.oauth2.client.refreshTokenValiditySeconds'),
      minWidth: 150,
      formatter: ({ cellValue }) => `${cellValue} 秒`,
    },
    {
      field: 'authorizedGrantTypes',
      title: $t('views.system.oauth2.client.authorizedGrantTypes'),
      minWidth: 100,
    },
    {
      field: 'createTime',
      title: $t('common.createTime'),
      minWidth: 180,
      formatter: 'formatDateTime',
    },
    {
      title: $t('common.action'),
      width: 130,
      fixed: 'right',
      slots: { default: 'actions' },
    },
  ];
}
